feat(cli): add shell flag-value completion for --checks/--export/--proxy-type - #59
Merged
Merged
Conversation
…oxy-type Cobra already ships a completion command that completes subcommands and flag names. It cannot, however, suggest values for flags whose accepted set is domain-specific: --checks <Tab> fell back to file completion. Register flag-value completion functions via cobra.OnInitialize (deferred to Execute time so the flags exist when registration runs, since package init order is alphabetical and completion.go runs before the flags are defined): - --checks: default check IDs + their categories + the literal "all", with comma-segment completion so only the final segment is narrowed. - --export: text/json/html/markdown. - --proxy-type: auto/http/https/socks4/socks5. Adds unit tests for each helper (prefix filtering, comma handling, exhaustive value sets) and a README Shell Completion section with per-shell install instructions. Closes francomano#22
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Closes #22 — shell completion support.
Background
Cobra already generates a
completioncommand (bash/zsh/fish/powershell) that outputs valid scripts and completes subcommands + flag names. That covers two of the three acceptance criteria out of the box. The real gap is the third criterion ("completions work with all subcommands and flags"): flag values are not completed.diagnose --checks <Tab>fell back to file completion, and--export/--proxy-typelikewise.Changes
cmd/cli/commands/completion.go— registers flag-value completion functions:--checks: default check IDs + their categories + the literalall, with comma-segment completion (only the final segment is narrowed, preserving the typed prefix).--export:text/json/html/markdown.--proxy-type:auto/http/https/socks4/socks5.cmd/cli/commands/completion_test.go— unit tests for each helper (prefix filtering, comma handling, exhaustive value sets, NoFileComp directive).README.md— new "Shell Completion" section with per-shell install instructions.Registration uses
cobra.OnInitializerather than a bareinit(): Go runs packageinit()s in filename order, socompletion.goruns beforediagnose.go/plugins.godefine the--checks/--export/--proxy-typeflags, andRegisterFlagCompletionFuncrejects a missing flag. Deferring to Execute time runs after everyinit()has completed.Verification
go build ./cmd/cli✅go test ./cmd/cli/commands/...✅__completehidden command:__complete diagnose --checks d→dns_leak,dns_resolve(:4NoFileComp)__complete diagnose --checks public_ip,dns→public_ip,dns_leak,public_ip,dns_resolve__complete diagnose --proxy-type s→socks4,socks5__complete diagnose --export m→markdowngo test ./...shows one failure incore/adapters(TestAdapterHTTPSProxy_HTTPRequest, an x509 cert-verification error in a local HTTPS-proxy fixture). It reproduces identically onmasterwithout these changes and is unrelated tocmd/cli/commands.Note
The completion script is keyed to the root command name (
proxyctl); the README section calls this out for anyone invoking the binary under a different name.